package cn.com.controller;

import cn.com.domain.LeaveEntity;
import cn.com.mapper.LeaveMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.com.Util.CopyUtils;
import cn.com.domain.dto.LeaveDTO;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;
import cn.com.service.LeaveService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假管理
 *
 * @Author CRL
 * @Date 2021/4/13 23:32
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/leave")
public class LeaveController {

    @Autowired
    LeaveService leaveService;

    @Autowired
    LeaveMapper leaveMapper;

    @PostMapping
    public BaseResponse<LeaveEntity> creat(@RequestBody(required = false) LeaveDTO requestDto) {
        LeaveEntity leaveEntity = new LeaveEntity();
        CopyUtils.copyProperties(requestDto, leaveEntity);
        leaveEntity.setCreateTime(new Date());
        boolean save = leaveService.save(leaveEntity);
        if (save) {
            return new BaseResponse(leaveEntity);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    @DeleteMapping("/{id}")
    public BaseResponse delete(@PathVariable("id") String id) {
        int i = leaveMapper.deleteById(id);
        if (i == 1) {
            return new BaseResponse(BaseResponseStatus.OK, "删除成功");
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "删除失败");
        }
    }

    @PutMapping("/{id}")
    public BaseResponse update(@PathVariable("id") String id, @RequestBody(required = false) LeaveDTO requestDto) {
        Map<String, Object> map = new HashMap(1);
        map.put("id", id);
        List<LeaveEntity> backlogEntities = leaveMapper.selectByMap(map);
        LeaveEntity leaveEntity = backlogEntities.get(0);
        CopyUtils.copyProperties(requestDto, leaveEntity);
        leaveEntity.setUpdateTime(new Date());
        boolean b = leaveService.saveOrUpdate(leaveEntity);
        if (b) {
            return new BaseResponse<>(requestDto);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "更新失败");
        }
    }

    @PostMapping("/query")
    public BaseResponse query(@RequestBody(required = false) LeaveDTO requestDto) {
        Integer pageNo = requestDto.getPageNo();
        Integer pageSize = requestDto.getPageSize();
        IPage<LeaveEntity> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<LeaveEntity> wrapper = new QueryWrapper<>();
        CopyUtils.copyProperties(requestDto, wrapper);
        IPage<LeaveEntity> leaveEntityIPage = leaveMapper.selectPage(userPage, wrapper);
        return new BaseResponse(leaveEntityIPage);
    }

    @GetMapping("/{id}")
    public BaseResponse findOneById(@PathVariable("id") String id) {
        LeaveEntity serviceById = leaveService.getById(id);
        return new BaseResponse(serviceById);
    }

}
